<?php
namespace Admin\Controller;
use Think\AjaxPage;
use Think\Controller;

class OrderController extends CommonController {

    public function _initialize() {
        $params = array("ajax_logs_list");
        parent::_initialize($params);
    }

    //订单列表
    public function index() {

        //当执行查询的时候
        $search = I('get.btn');
        $export =  I('get.export');
        if ($search || $export) {
            $start = strtotime(trim(I('get.start_time')));
            $end = strtotime(trim(I('get.end_time')));

            //填写下单时间
            if ($start && $end) {
                $map['time'] = array('between', array($start, $end));
            } elseif($start) {
                $map['time'] = array('EGT', $start);
            } elseif($end) {
                $map['time'] = array('ELT', $end);
            }

            //选择订单状态
            $status = I('get.status');
            if ($status != -2) {
                if ($status == -1) {
                    $map['status']  = array('not in','-1,3');
                } elseif ($status == -3) {
                    $map['status'] = -1;
                } else {
                    $map['status'] = $status;
                }
                $this->assign('x_status', $status);
            }

            //下单用户名或手机号码
            $u_name = trim(I('get.user_name'));
            if ($u_name) {

                $bool = preg_match("/^1[34578]{1}\d{9}$/",$u_name);
                if($bool){
                    $map['phone'] = $u_name;
                }else{
                    $map['consignee'] = array('like','%'.$u_name.'%');
                }
                $this->assign('u_name', $u_name);
            }

            //下单人用户名
            $user_phone = trim(I('get.user_phone'));
            if ($user_phone) {
                $where['agent_name'] = array('like','%'.$user_phone.'%');
                $agent_id = M('agent')->field('agent_id')->where($where)->select();
                $agent_ids ='';
                foreach ($agent_id as $v) {
                    $agent_ids .= $v['agent_id'].',';
                }

                $agent_ids = substr($agent_ids,0,strlen($agent_ids)-1);
                $map['uid'] = array('in', $agent_ids);
                $this->assign('u_phone', $user_phone);
            }

            //选择付款状态
            $is_pay = I('get.is_pay');
            if ($is_pay != -2) {
                $map['is_pay'] = $is_pay;
                $this->assign('x_pay', $is_pay);
            }

            //发货状态
            $is_deliver = I('get.is_deliver');
            if ($is_deliver != -2) {
                if ($is_deliver == 2) {
                    $map['is_platform'] = 1;//待平台发货
                    $map['is_deliver'] = -1;
                } else {
                    $map['is_deliver'] = $is_deliver;
                }
                $this->assign('x_deliver', $is_deliver);
            }

            //订单编号
            $order_id = I('get.order_id');
            if ($order_id) {
                $map['order_id'] = $order_id;
                $this->assign('orderId', $order_id);
            }

            //店铺名字
            $shop_name = trim(I("get.shop_name"));
            $shop_id = to_int(I("get.shop_id"));
            if($shop_name){
                $map['shop_id'] = $shop_id;
                $this->assign("shop_name",$shop_name);
            }
        }

        //查询分页
        $total = D('Order')->relation(true)->where($map)->count();
        $page = set_page($total, C('PAGE_NUM'));

        foreach($map as $key=>$val) {
            $page->parameter[$key] = urlencode($val);
        }
        $show = $page->show();
        $order = D('Order')->order('id DESC')->relation(true)->where($map)->select();

        //当执行导出数据的时候
        if ($export) {

            //把商品信息链接
            foreach ($order as $k=>$v) {
                $order[$k]['goods'] = M('orderGood')
                    ->alias('a')->join('LEFT JOIN __GOOD__ b ON a.good_id=b.id')
                    ->where( array('a.order_id'=>$v['id']) )
                    ->select();
            }

            $order = D('order')->format($order);


            $strTable ='<table width="500" border="1">';
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:center;font-size:12px;width:120px;font-weight: bold;">订单编号</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;"width="100">下单人</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">收货人</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">收货人号码</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">支付状态</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">订单状态</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">发货状态</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">下单时间</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">运费</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">收货地址</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">商品名称</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">商品数量</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">单价</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">小计</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">订单总价(含运费)</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">商品货号</td>';
            $strTable .= '</tr>';

            foreach ($order as $key=>$val) {
                $address = $val['province'].$val['city'].$val['area'].$val['detail_address'];
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['order_id'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['agent_name'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['consignee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['mobile'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['is_pay'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['status'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['is_deliver'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['freight_price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$address.'</td>';
                $g_names = $good_sns = $num_s = $good_price = $avg = '';
                foreach($order[$key]['goods'] as $goods) {
                    $g_names .= $goods['g_name']."<br />";
                    $good_sns .= $goods['good_sn']."<br />";
                    $num_s .= $goods['num']."<br />";
                    $good_price .= $goods['goods_price']."<br />";
                    $avg .= ($goods['goods_price']/$goods['num'])."<br />";
                }

                $strTable .= '<td style="text-align:left;font-size:12px;">'.$g_names.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$num_s.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$avg.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$good_price.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$good_sns.' </td>';
                $strTable .= '</tr>';
            }
            $strTable .='</table>';
            createExcel($strTable, '微商系统订单');

        } else {
            $order = D('Order')->order('id DESC')->relation(true)->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        }

        //订单状态
        $order_status = array('-1' => '进行中', '3' => '交易成功', '-3'=>'已取消');
        //付款状态
        $pay_status = array('-1' => '未付款', '1' => '已付款');
        //发货状态
        $deliver_status = array('-1' => '未发货', '1' => '已发货', '2'=>'待平台发货');
        //店铺的列表
        $shops = M('shop')->field('shop_name,id')->order( array('id DESC') )->select();
        $this->assign('shops',$shops);
        $this->assign('start_time',I('get.start_time'));
        $this->assign('end_time',I('get.end_time'));
        $this->assign('deliver_status', $deliver_status);
        $this->assign('pay_status', $pay_status);
        $this->assign('order_status', $order_status);
        $this->assign('page',$show);
        $this->assign('orders', $order);
        $this->display();

    }

    //订单详情
    public function detail() {
        //订单的基本信息和收货信息
        $map['id'] = I('get.id');
        $order = D('Order')->order('id DESC')->relation(true)->where($map)->find();
        //当订单完成并且是代理商品的时候，计算返利
        if($order['status'] == 3 && $order['is_platform'] == 0) {
            $rebate = M('rebate')->alias('a')
                ->join('LEFT JOIN __AGENT__ b ON a.re_agent=b.agent_id')
                ->where( array('order_id'=>$order['id']) )
                ->order('rebate_id ASC')
                ->select();
            $this->assign('rebate_num', count($rebate));
            $this->assign('rebate', $rebate);
        }
        //订单里面的商品
        $orderGood = M('orderGood')->alias('a')->join('LEFT JOIN __GOOD__ ON a.good_id=__GOOD__.id')->where( array('a.order_id'=>I('get.id')) )->select();
        //订单流向
        $id = I('get.id');
        $orderThrough = D('OrderThrough')->relation(true)->where( array('order_id'=>$id) )->order('id ASC')->select();
        //计算订单的总价格
        $sum = 0;
        foreach ($orderGood as $v) {
            $sum += $v['num'] * $v['price'];
        }
        if ($order['discount'] != 0) {
            $endSum = $sum - $order['discount'];
        }
        $pay_type = M("payment")->where("p_id = %d" ,array($order["pay_type"]))->getField("p_name");
        //获取操作纪录
        $order_record = D('OrderRecord')->relation(true)->where( array('order_id'=>I('get.id')) )->select();

        $app = date("Y-m-d H:i:s",($order['deliver_time']));
        $this->assign('orderRecord', $order_record);
        $this->assign('num', count($orderGood));
        $this->assign('sum', $sum);
        $this->assign('endSum', $endSum);
        $this->assign('orderGoods', $orderGood);
        $this->assign('order', $order);
        $this->assign('pay_type', $pay_type);
        $this->assign('orderThrough', $orderThrough);
        $this->assign('num', count($orderThrough));
        $this->display();
    }

    public function selectExcel() {

        $data=import_excel('./Uploads/excel/1.csv');
        print_r($data);
    }

    //发货单
    public function  invoice(){
        //订单的基本信息和收货信息
        $map['id'] = I('get.id');
        $order = D('Order')->order('id DESC')->relation(true)->where($map)->find();
        //下单人
        $buyer = M('agent')->where(array('agent_id'=>$order['uid']))->find();
        //支付方式
        $p_name = M('payment')->where(array('p_id'=>$order['pay_type']))->getField('p_name');
        //发货物流
        $logistics = M('express')->where(array('id'=>$order['send_type']))->getField('e_name');
        //订单里面的商品
        $orderGood = M('orderGood')->alias('a')->join('LEFT JOIN __GOOD__ ON a.good_id=__GOOD__.id')->where( array('a.order_id'=>I('get.id')) )->select();
        $data = M('order_record')->where(array('uid'=>session('role_id'),'order_id'=>$map['id']))->find();

        if(!$data){
            $consignor['name'] = session('name');
            $consignor['time'] = date("Y-m-d H:i:s");
            $this->assign('consignor',$consignor);
        }else{
            $data['name'] = M('manager')->where(array('id'=>session('role_id')))->getField('name');
            $data['time'] =date("Y-m-d H:i:s",$data['time']);
            $this->assign('consignor',$data);
        }
        $this->assign('orderGoods',$orderGood);
        $this->assign('buyer',$buyer);
        $this->assign('logistics',$logistics);
        $this->assign('data',$data);
        $this->assign('order', $order);
        $this->assign('p_name',$p_name);
        $this->display();
    }

    //退货单列表
    public function return_list()
    {
        //搜索
        if(IS_POST){
            $order_status = I("post.order_status");
            if($order_status){
                $where['order_status'] = $order_status;
            }
            $order_num = I("post.order_num");
            if($order_num){
                $where['order_id'] = M('order')->where(array('order_id'=>$order_num))->getField('id');
            }
            //店铺名字
            $shop_name = trim(I("post.shop_name"));
            $shop_id = to_int(I("post.shop_id"));
            if($shop_name) {
                $where['shop_id'] = $shop_id;
                $this->assign("shop_name", $shop_name);
            }
            $end_time = strtotime(I("post.end_time"));
            $start_time =strtotime(I("post.start_time"));
            if($end_time||$start_time){
                $count = M('back_order')->where($where)->where("$start_time < time AND $end_time > time")->count();
                $page = set_page($count, C('PAGE_NUM'));
                $show = $page->show();
                $res = M('back_order')->where($where)->where("$start_time < time AND $end_time > time")
                    ->limit($page->firstRow.','.$page->listRows)->select();
            }else{
                $count = M('back_order')->where($where)->count();
                $page = set_page($count, C('PAGE_NUM'));
                $show = $page->show();
                $res = M('back_order')->where($where)
                    ->limit($page->firstRow.','.$page->listRows)->select();
            }

            foreach ($res as $key=>$value){
                $res[$key]['t_order_id'] = M('order')->where(array('id'=>$value['order_id']))->getField('order_id');
                $uid =  M('order')->where(array('id'=>$value['order_id']))->getField('uid');
                $res[$key]['name'] = M('agent')->where(array('agent_id'=>$uid))->getField('agent_name');
                $send_type = M('order')->where(array('id'=>$value['order_id']))->getField('send_type');
                $res[$key]['send_type'] = M('express')->where(array('id'=>$send_type))->getField('e_name');
                $res[$key]['consignee'] = M('order')->where(array('id'=>$value['order_id']))->getField('consignee');
                $res[$key]['user_phone'] = M('order')->where(array('id'=>$value['order_id']))->getField('user_phone');
                $res[$key]['price'] = M('order')->where(array('id'=>$value['order_id']))->getField('price');
                $res[$key]['back_time'] = date("Y-m-d H:i:s",$value['time']);
                $res[$key]['back_status'] = $value['order_status'];
                $res[$key]['shop_name'] = $shop_name;
            }
            $data['order_status'] = $order_status;
            $data['order_num'] = $order_num;
            if($start_time||$end_time){
                $data['start_time'] =  date("Y-m-d H:i:s",$start_time);
                $data['end_time'] = date("Y-m-d H:i:s",$end_time);
            }
            $this->assign('data',$data);
            $this->assign('page',$show);
            $this->assign('count',$count);
            $this->assign('order_info',$res);

            $this->display();

        }else{
            //读取
            $count = M('back_order')->count();
            $page = set_page($count, C('PAGE_NUM'));
            $show = $page->show();
            $res = M('back_order')->order('time DESC')->limit($page->firstRow.','.$page->listRows)->select();
            foreach ($res as $key=>$value){
                $res[$key]['t_order_id'] = M('order')->where(array('id'=>$value['order_id']))->getField('order_id');
                $uid =  M('order')->where(array('id'=>$value['order_id']))->getField('uid');
                $res[$key]['name'] = M('agent')->where(array('agent_id'=>$uid))->getField('agent_name');
                $send_type = M('order')->where(array('id'=>$value['order_id']))->getField('send_type');
                $res[$key]['send_type'] = M('express')->where(array('id'=>$send_type))->getField('e_name');
                $res[$key]['consignee'] = M('order')->where(array('id'=>$value['order_id']))->getField('consignee');
                $res[$key]['user_phone'] = M('order')->where(array('id'=>$value['order_id']))->getField('user_phone');
                $res[$key]['price'] = M('order')->where(array('id'=>$value['order_id']))->getField('price');
                $res[$key]['back_time'] = date("Y-m-d H:i:s",$value['time']);
                $res[$key]['back_status'] = $value['order_status'];
                $shop_id = M('order')->where(array("id"=>$value['order_id']))->getField("shop_id");
                $res[$key]['shop_name'] = M("shop")->where(array("id"=>$shop_id))->getField("shop_name");
            }

            $this->assign('page',$show);

            $this->assign('count',$count);
            $this->assign('order_info',$res);
            $this->display();
        }

    }

    public function return_good_info()
    {
        $order_id = I("get.order_id");
        $good_id = I("get.good_id");
        $t_order_id = M('order')->where(array('id'=>$order_id))->getField('order_id');
        $uid = M('order')->where(array('id'=>$order_id))->getField('uid');
        $name = M('agent')->where(array('agent_id'=>$uid))->getField('agent_name');
        $info =  M('back_order')->where(array('order_id'=>$order_id,'good_id'=>$good_id))->find();
        $info['name'] = $name;
        $info['t_order_id'] = $t_order_id;
        $info['time'] = date("Y-m-d H:i:s",$info['time']);
        if(!empty($info['judge_time'])){
            $info['judge_time'] = date("Y-m-d H:i:s",$info['judge_time']);
        }
        $pics = M('back_img')->where(array('order_id'=>$order_id,'good_id'=>$good_id))->select();

        $info['img'] = M('good')->where(array('id'=>$info['good_id']))->getField('img');
        $info['name'] = M('good')->where(array('id'=>$info['good_id']))->getField('g_name');

        $this->assign('pics',$pics);
        $this->assign('info',$info);
        $this->display();
    }

    //待平台发货订单
    public function ready_send(){
        //当执行查询的时候
        $search = I('get.btn');
        $export =  I('get.export');
        if ($search || $export) {
            $start = strtotime(trim(I('get.start_time')));
            $end = strtotime(trim(I('get.end_time')));

            //填写下单时间
            if ($start && $end) {
                $map['time'] = array('between', array($start, $end));
            } elseif($start) {
                $map['time'] = array('EGT', $start);
            } elseif($end) {
                $map['time'] = array('ELT', $end);
            }

            //店铺名字
            $shop_name = trim(I("get.shop_name"));
            $shop_id = to_int(I("get.shop_id"));
            if($shop_name) {
                $map['shop_id'] = $shop_id;
                $this->assign("shop_name", $shop_name);
            }

            //下单用户名或手机号码
            $u_name = trim(I('get.user_name'));
            if ($u_name) {

                $bool = preg_match("/^1[34578]{1}\d{9}$/",$u_name);
                if($bool){
                    $map['phone'] = $u_name;
                }else{
                    $map['consignee'] = array('like','%'.$u_name.'%');
                }
                $this->assign('u_name', $u_name);
            }

            //下单人用户名
            $user_phone = trim(I('get.user_phone'));
            if ($user_phone) {
                $where['agent_name'] = array('like','%'.$user_phone.'%');
                $agent_id = M('agent')->field('agent_id')->where($where)->select();
                $agent_ids ='';
                foreach ($agent_id as $v) {
                    $agent_ids .= $v['agent_id'].',';
                }

                $agent_ids = substr($agent_ids,0,strlen($agent_ids)-1);
                $map['uid'] = array('in', $agent_ids);
                $this->assign('u_phone', $user_phone);
            }



            //订单编号
            $order_id = I('get.order_id');
            if ($order_id) {
                $map['order_id'] = $order_id;
                $this->assign('orderId', $order_id);
            }
        }

        $map['is_platform'] = 1;//待平台发货
        $map['is_deliver'] = -1;
        $map['is_pay'] = 1;


        //查询分页
        $total = D('Order')->relation(true)->where($map)->count();
        $page = set_page($total, C('PAGE_NUM'));

        foreach($map as $key=>$val) {
            $page->parameter[$key] = urlencode($val);
        }
        $show = $page->show();
        $order = D('Order')->order('id DESC')->relation(true)->where($map)->select();


        //当执行导出数据的时候
        if ($export) {

            //把商品信息链接
            foreach ($order as $k=>$v) {
                $order[$k]['goods'] = M('orderGood')
                    ->alias('a')->join('LEFT JOIN __GOOD__ b ON a.good_id=b.id')
                    ->where( array('a.order_id'=>$v['id']) )
                    ->select();
            }

            $order = D('order')->format($order);


            $strTable ='<table width="500" border="1">';
            $strTable .= '<tr>';
            $strTable .= '<td style="text-align:center;font-size:12px;width:120px;font-weight: bold;">订单编号</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;"width="100">下单人</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">收货人</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">收货人号码</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">支付状态</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">订单状态</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">发货状态</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">下单时间</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">运费</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">收货地址</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">商品名称</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">商品数量</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">单价</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">小计</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">订单总价(含运费)</td>';
            $strTable .= '<td style="text-align:center;font-size:12px;font-weight: bold;" width="*">商品货号</td>';
            $strTable .= '</tr>';

            foreach ($order as $key=>$val) {
                $address = $val['province'].$val['city'].$val['area'].$val['detail_address'];
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['order_id'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['agent_name'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['consignee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['mobile'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['is_pay'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['status'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['is_deliver'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['freight_price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$address.'</td>';
                $g_names = $good_sns = $num_s = $good_price = $avg = '';
                foreach($order[$key]['goods'] as $goods) {
                    $g_names .= $goods['g_name']."<br />";
                    $good_sns .= $goods['good_sn']."<br />";
                    $num_s .= $goods['num']."<br />";
                    $good_price .= $goods['goods_price']."<br />";
                    $avg .= ($goods['goods_price']/$goods['num'])."<br />";
                }

                $strTable .= '<td style="text-align:left;font-size:12px;">'.$g_names.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$num_s.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$avg.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$good_price.' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$good_sns.' </td>';
                $strTable .= '</tr>';
            }
            $strTable .='</table>';
            createExcel($strTable, '微商系统订单');

        } else {
            $order = D('Order')->order('id DESC')->relation(true)->where($map)->limit($page->firstRow.','.$page->listRows)->select();
        }

        $this->assign('start_time',I('get.start_time'));
        $this->assign('end_time',I('get.end_time'));
        $this->assign('page',$show);
        $this->assign('orders', $order);
        $this->display();
    }

    //发货单
    public function  send_order(){
        //当执行查询的时候
        $search = I('get.btn');
        $export =  I('get.export');
        if ($search || $export) {
            $start = strtotime(trim(I('get.start_time')));
            $end = strtotime(trim(I('get.end_time')));

            //订单编号
            $order_id = I('get.order_id');
            if ($order_id) {
                $map['order_id'] = $order_id;
                $this->assign('orderId', $order_id);
            }


            //店铺名字
            $shop_name = trim(I("get.shop_name"));
            $shop_id = to_int(I("get.shop_id"));
            if($shop_name) {
                $map['shop_id'] = $shop_id;
                $this->assign("shop_name", $shop_name);
            }

            //填写下单时间
            if ($start && $end) {
                $map['time'] = array('between', array($start, $end));
            } elseif ($start) {
                $map['time'] = array('EGT', $start);
            } elseif ($end) {
                $map['time'] = array('ELT', $end);
            }


            //下单用户名或手机号码
            $u_name = trim(I('get.user_name'));
            if ($u_name) {

                $bool = preg_match("/^1[34578]{1}\d{9}$/", $u_name);
                if ($bool) {
                    $map['phone'] = $u_name;
                } else {
                    $map['consignee'] = array('like', '%' . $u_name . '%');
                }
                $this->assign('u_name', $u_name);
            }
        }

        $map['is_deliver'] = '1';
        $map['is_platform'] = '1';

        //查询分页
        $total = D('order')->relation(true)->where($map)->count();
        $page = set_page($total, C('PAGE_NUM'));
        $show = $page->show();

        $res = D('order')->relation(true)->where(array($map))->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($res as $key=>$value){
            $res[$key]['send_type'] = M('express')->where(array('id'=>$value['send_type']))->getField('e_name');
            $res[$key]['time'] = date("Y-m-d H:i:s",$value['time']);
            $res[$key]['deliver_time'] = date("Y-m-d H:i:s",$value['deliver_time']);
            $res[$key]['pay_type'] = M('payment')->where(array('p_id'=>$value['pay_type']))->getField('p_name');
        }
        $this->assign('orders',$res);
        $this->assign('page',$show);
        $this->display();
    }

    public function searchShopName() {
        $shop_name = trim(I('post.aName'));
        if($shop_name != ''){
            $shopList = M("shop")->where("shop_name LIKE '%$shop_name%'  AND state = '1'")->select();
            if(!empty($shopList)) {
                $list = $shopList;
            } else {
                $list = M("shop")->field('shop_name, id')->select();
            }
            $this->ajaxReturn($list);
        }
    }

}